const {resolve} = require('path');
const WebpackHtmlPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './index.js',
  output: {
    filename: 'built.js',
    path: resolve(__dirname, 'build')
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /\.(jpg|png|gif)$/,
        loader: 'url-loader',
        options: {
          limit: 8 * 1024,
          esModule: false,
          name: '[hash:10].[ext]'
        }
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
        options: {
          esModule: false
        }
      },
      {
        // 其他文件引入
        // exclude 排除
        exclude: /\.(css|js|html|less|scss|jpg|png|gif)/,
        loader: 'file-loader',
        options: {
          name: '[hash:10].[ext]'
        }
      }
    ]
  },
  plugins: [
    new WebpackHtmlPlugin({
      template: './index.html'
    })
  ],
  mode: 'development',
  
  // 开发服务器，用来自动化（自动编译、自动打开浏览器，自动刷新浏览器）
  // 特点：只会在内存中编译打包，不会有任何输出
  // 启动dev-server指令：npx webpack serve
  devServer: {
    // 项目构建后的路径
    contentBase: resolve(__dirname, 'build'),
    //启用gzip压缩
    compress: true,
    //端口号
    port: 3001,
    //自动打开浏览器
    open:true
  }

}